﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using EventPlanner.Models;
using System.Xml.Linq;
using System.Xml;
using System.Runtime.Serialization.Json;

namespace EventPlanner.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "EventosWCF" in code, svc and config file together.
    public class EventosWCF : IEventosWCF
    {
        EventPlannerContext context = new EventPlannerContext();

        public XmlElement UnEvento(int id)
        {
            XDocument doc = new XDocument();
            Uri uri = OperationContext.Current.IncomingMessageHeaders.To;
            string server = uri.Host + ":" + uri.Port;
            Evento e = (from item in context.Evento where item.id == id select item).First();
            XElement evt = new XElement("Evento");
            evt.Add(new XAttribute("id", e.id));
            evt.Add(new XElement("Nombre", e.Nombre));
            evt.Add(new XElement("Lugar", e.Lugar));
            evt.Add(new XElement("Descripcion", e.Descripcion));
            evt.Add(new XElement("Fecha", e.Fecha.ToShortDateString()));
            evt.Add(new XElement("Hora", e.Fecha.ToShortTimeString()));
            evt.Add(new XElement("Organizador", e.Organizador.Nombre));
            evt.Add(new XElement("Capacidad", "" + e.Participantes.Count + "/" + e.Cupo));
            evt.Add(new XElement("Link", "http://" + server + "/Evento/DetailsTodos/" + e.id));
            doc.Add(evt);
            XmlDocument resultDocument = new XmlDocument();
            resultDocument.Load(doc.CreateReader());
            return resultDocument.DocumentElement;
        }

        public XmlElement Listado(int cant, int comienzo)
        {
            XDocument doc = new XDocument();
            XElement lista = new XElement("Listado");
            Uri uri = OperationContext.Current.IncomingMessageHeaders.To;
            string server = uri.Host + ":" + uri.Port;
            foreach (Evento e in (from item in context.Evento orderby item.Nombre select item).Skip(comienzo).Take(cant))
            {
                XElement evt = new XElement("Evento");
                evt.Add(new XAttribute("id", e.id));
                evt.Add(new XElement("Nombre", e.Nombre));
                evt.Add(new XElement("Lugar", e.Lugar));
                evt.Add(new XElement("Fecha", e.Fecha.ToShortDateString()));
                evt.Add(new XElement("Hora", e.Fecha.ToShortTimeString()));
                evt.Add(new XElement("Organizador", e.Organizador.Nombre));
                evt.Add(new XElement("Capacidad", "" + e.Participantes.Count + "/" + e.Cupo));
                evt.Add(new XElement("Link","http://"+server+"/Evento/DetailsTodos/"+e.id));
                lista.Add(evt);
            }
            doc.Add(lista);
            XmlDocument resultDocument = new XmlDocument();
            resultDocument.Load(doc.CreateReader());
            return resultDocument.DocumentElement;
        }

        public XmlElement Busqueda(string busqueda, int cant)
        {
            XDocument doc = new XDocument();
            XElement lista = new XElement("Listado");
            Uri uri = OperationContext.Current.IncomingMessageHeaders.To;
            string server = uri.Host + ":" + uri.Port;
            foreach (Evento e in (from item in context.Evento where (item.Nombre.ToUpper().Contains(busqueda.ToUpper()) || busqueda=="") orderby item.Nombre select item).Take(cant))
            {
                XElement evt = new XElement("Evento");
                evt.Add(new XAttribute("id", e.id));
                evt.Add(new XElement("Nombre", e.Nombre));
                evt.Add(new XElement("Lugar", e.Lugar));
                evt.Add(new XElement("Fecha", e.Fecha.ToShortDateString()));
                evt.Add(new XElement("Hora", e.Fecha.ToShortTimeString()));
                evt.Add(new XElement("Organizador", e.Organizador.Nombre));
                evt.Add(new XElement("Capacidad", "" + e.Participantes.Count + "/" + e.Cupo));
                evt.Add(new XElement("Link", "http://" + server + "/Evento/DetailsTodos/" + e.id));
                lista.Add(evt);
            }
            doc.Add(lista);
            XmlDocument resultDocument = new XmlDocument();
            resultDocument.Load(doc.CreateReader());
            return resultDocument.DocumentElement;
        }
    }
}
